package com.liuda.workbench.controller;

import com.liuda.settings.domain.DicValue;
import com.liuda.settings.domain.User;
import com.liuda.settings.service.DicValueService;
import com.liuda.settings.service.UserService;
import com.liuda.workbench.domain.Activity;
import com.liuda.workbench.domain.Contact;
import com.liuda.workbench.domain.ContactRemark;
import com.liuda.workbench.domain.Transfer;
import com.liuda.workbench.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ResourceBundle;

@Controller
public class ContactsController {
    @Autowired
    ContactService contactService;
    @Autowired
    DicValueService dicValueService;
    @Autowired
    UserService userService;
    @Autowired
    ContactRemarkService contactRemarkService;
    @Autowired
    TransferService transferService;
    @Autowired
    ActivityService activityService;
    @RequestMapping("/workbench/contacts/toIndex.do")
    public String toIndex(HttpServletRequest request){
        List<Contact> contactList= contactService.selectAllForDetail();
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> appellationList=dicValueService.queryDicValueByTypeCode("appellation");
        List<User> userList=userService.getAllUnlockedUsers();
        request.setAttribute("contactList",contactList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("userList",userList);
        return "workbench/contacts/index";
    }
    @RequestMapping("/workbench/contacts/toDetail.do")
    public String toDetail(String id,HttpServletRequest request){
        Contact contact=contactService.selectForDetailById(id);
        List<ContactRemark> contactRemarkList=contactRemarkService.selectByContactsId(id);
        List<Transfer> transactionList=transferService.selectByContactsId(id);
        List<Activity> activityList=activityService.selectByContactsId(id);
        ResourceBundle bundle=ResourceBundle.getBundle("possibility");
        for (Transfer transfer : transactionList) {
            transfer.setPossibility(bundle.getString(transfer.getStage()));
        }
        request.setAttribute("contact",contact);
        request.setAttribute("contactRemarkList",contactRemarkList);
        request.setAttribute("transactionList",transactionList);
        request.setAttribute("activityList",activityList);
        return "workbench/contacts/detail";
    }
    @ResponseBody
    @RequestMapping("/workbench/contacts/queryByLike.do")
    public Object queryByLike(String name){
        return contactService.selectByLike(name);
    }
}
